use std::fs;
use std::path::Path;

use serde::{Deserialize, Serialize};
use tracing::info;
use crate::constants::ENV_PROFILES_ACTIVE_KEY;

#[derive(Serialize, Deserialize, Debug)]
pub struct AppConfig {
    pub app_name: String,
    pub profiles: Profiles,
    pub server: ServerConfig,
    pub db: DBConfig,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Profiles {
    pub active: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DBConfig {
    pub host: String,
    pub port: u16,
    pub name: String,
    pub url: String,
    pub user: String,
    pub pwd: String,
    pub db_type: String,
}

impl AppConfig {
    pub fn new() -> Self {
        let app_active_env = std::env::var(ENV_PROFILES_ACTIVE_KEY.as_str()).unwrap_or("dev".to_string());

        if app_active_env.as_str() == "dev" {

        }
        let yaml_path = format!("config/app-{}.yaml", app_active_env);
        let path = Path::new(yaml_path.as_str());
        let yaml_str = fs::read_to_string(path).expect(format!("缺少yaml配置文件-{}", yaml_path).as_str());
        info!(">>>>>加载配置文件: {}", yaml_path);

        let app_config: AppConfig = serde_yaml::from_str(yaml_str.as_str()).unwrap();

        info!("APP_NAME: {}", app_config.app_name);
        info!("SERVER PROFILES_ACTIVE: {}", app_config.profiles.active);
        let host = match app_config.server.host == "0.0.0.0".to_string() {
            true => "127.0.0.1",
            false => app_config.server.host.as_str(),
        };
        info!("SERVER URL: http://{}:{}", host, app_config.server.port);

        app_config
    }
}

/*impl Default for AppConfig {
    fn default() -> Self {
        let app_active_env = std::env::var("YM_APP_ACTIVE_ENV").unwrap_or("dev".to_string());

        let env_path = format!(".env.{}", app_active_env);
        println!(">>>>>加载配置文件: {}", env_path);
        dotenv::from_filename(env_path.as_str()).expect("缺少配置文件");
        dotenv::from_filename(".env").expect("缺少配置文件");

        let server_host = std::env::var("SERVER_HOST").unwrap_or("0.0.0.0".to_string());
        let server_port: u16 = std::env::var("SERVER_PORT")
            .unwrap_or_else(|_| "3000".to_string())
            .parse()
            .unwrap();
        let server_config = ServerConfig {
            host: server_host,
            port: server_port,
        };

        let db_config = DBConfig {
            host: std::env::var("DB_HOST").unwrap_or("localhost".to_string()),
            port: std::env::var("DB_PORT").unwrap_or("3060".to_string()).parse().unwrap(),
            name: std::env::var("DB_NAME").unwrap_or("ym_test".to_string()),
            url: std::env::var("DB_URL").unwrap_or("localhost".to_string()),
            user: std::env::var("DB_USER").unwrap_or("root".to_string()),
            pwd: std::env::var("DB_PWD").unwrap_or("".to_string()),
            db_type: std::env::var("DB_TYPE").unwrap_or("mysql".to_string()),
        };

        AppConfig {
            server: server_config,
            db: db_config,
        }
    }
}*/

/*impl AppConfig {
    pub fn instance<'a>() -> &'a Self {
        lazy_static! {
            pub static ref CONFIG: AppConfig = AppConfig::default();
        }
        &CONFIG
    }
}*/
